﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TrainigWorkshop.Service;
using TrainigWorkshop.Model;
using System.Text.RegularExpressions;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Channels;
using TrainigWorkshop.Contract;

namespace Presentation
{
    public partial class UpdateStudent : System.Web.UI.Page
        
    {
        Boolean type;
        protected void Page_Load(object sender, EventArgs e)
        {
            type = Convert.ToBoolean(Request["type"]);
                if (!IsPostBack)
                {   
                    if (type==true)
                    {
                       BindClass();
                    }
                    else
                    {
                       BindClass();
                       BindStudentInfo();
                    }
                }
         }

        private void BindClass()
        {
            using (ChannelFactory<IProjectBService> channelFactory = new ChannelFactory<IProjectBService>("ProjectBClient"))
            {
                IProjectBService channel = channelFactory.CreateChannel();
                ddlClassName.DataSource = channel.GetAllClassInfo();
                ddlClassName.DataValueField = "ClassID";
                ddlClassName.DataTextField = "ClassName";
                ddlClassName.DataBind();
            }
        }

        private void BindStudentInfo()
        {
            using (ChannelFactory<IProjectBService> channelFactory = new ChannelFactory<IProjectBService>("ProjectBClient"))
            {
                IProjectBService channel = channelFactory.CreateChannel();
                int studentId = Convert.ToInt32(Request["ID"]);
                Grade grade = new Grade();

                //get student
                StudentModel student = channel.GetStudentInfoById(studentId);

                this.txtStudentName.Text = student.StudentName;
                if (student.StudentGender.Trim().ToLower() == "female")
                {
                    this.RadioBtnFemale.Checked = true;
                }
                else
                {
                    this.RadioBtnMale.Checked = true;
                }
                this.ddlClassName.SelectedValue = student.grades.ClassId.ToString().Trim();
            }
        }

        protected void BtnSave_Click(object sender, EventArgs e)
        { 
                string studentName = txtStudentName.Text.Trim();
                string regPatten = "^[A-Za-z]+$";
                Regex reg = new Regex(regPatten);
                if (studentName == "")
                {
                    Response.Write("<script>alert('Please fill a StudentName')</script>");
                }
                if (studentName.Length > 10)
                {
                    Response.Write("<script>alert('The length of StudentName should be less than 10')</script>");
                }
                if (reg.Match(studentName).Success == false)
                {
                    Response.Write("<script>alert('StudentName should be alphabet')</script>");
                }
                if (studentName != "" && studentName.Length <= 10)
                {
                    if (reg.Match(studentName).Success)
                    {    
                        if (type == true)
                        {
                            InsertStudentInfo();
                        }
                        else
                        {
                        UpdateStudentInfo();
                        }
                        Response.Redirect("StudentMainPage.aspx");
                    }
                }
        }

        private void InsertStudentInfo()
        {
            using (ChannelFactory<IProjectBService> channelFactory = new ChannelFactory<IProjectBService>("ProjectBClient"))
            {
                IProjectBService channel = channelFactory.CreateChannel();
                Grade grade = new Grade();
                StudentModel student = new StudentModel(grade);
                student.StudentName = txtStudentName.Text.Trim();
                student.StudentGender = (RadioBtnFemale.Checked == true) ? RadioBtnFemale.Text.Trim() : RadioBtnMale.Text.Trim();
                student.grades.ClassId = int.Parse(ddlClassName.SelectedValue.ToString());
                ProjectBService studentbll = new ProjectBService();
                studentbll.InsertStudentInfo(student);
            }
        }

        private void UpdateStudentInfo()
        {
            using (ChannelFactory<IProjectBService> channelFactory = new ChannelFactory<IProjectBService>("ProjectBClient"))
            {
                IProjectBService channel = channelFactory.CreateChannel();
                Grade grade = new Grade();
                StudentModel stu = new StudentModel(grade);
                stu.StudentName = txtStudentName.Text.Trim();
                stu.StudentGender = (RadioBtnFemale.Checked == true) ? RadioBtnFemale.Text.Trim() : RadioBtnMale.Text.Trim();
                stu.grades.ClassId = int.Parse(ddlClassName.SelectedValue);
                stu.StudentId = Convert.ToInt16(Request["ID"]);
                channel.UpdateStudentInfo(stu);
            }
        }
    }
}